
package com.ifcar.app.view;

import com.ifcar.app.util.GroupAdapter;
import com.ifcar.app.util.GroupAdapter.ItemPosition;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.FrameLayout;
import android.widget.ListView;

public class CommonGroupListView extends FrameLayout implements OnScrollListener, ListViewDelegator {

    private FrameLayout mTopView;

    private ListViewDelegator mListView;

    private int mCurrentGroup = -1;

    private TopViewCallback mTopViewCallback;

    // private boolean mDrawTop;

    public CommonGroupListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public CommonGroupListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CommonGroupListView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        final View list = findViewById(R.id.group_list);
        if (list instanceof ListViewDelegator) {
            mListView = (ListViewDelegator) list;
        } else if (list instanceof ListView) {
            mListView = new ListViewDelegator() {

                @Override
                public ListView getListView() {
                    return (ListView) list;
                }
            };
        } else {
            throw new IllegalArgumentException(
                    "R.id.list in GroupListView Only Support ListView Or ListViewDelegator");
        }
        
        mTopView = new FrameLayout(getContext());

        FrameLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT, Gravity.TOP);
        addView(mTopView, lp);

        getListView().setOnScrollListener(this);
    }

    public ListView getListView() {
        return mListView.getListView();
    }

    public void setTopViewCallback(TopViewCallback callback) {
        mTopViewCallback = callback;
        if (callback != null) {
            mTopView.removeAllViews();
            View top = callback.createView();
            if (top != null) {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) top.getLayoutParams();
                if (lp == null) {
                    lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                }
                mTopView.addView(top, lp);
            }

        } else {
            mTopView.removeAllViews();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        GroupAdapter adapter = (GroupAdapter) view.getAdapter();
        if (adapter == null || adapter.getCount() == 0 || getHeight() == 0) {
            mTopView.setVisibility(GONE);
            return;
        }
        ListView listView = getListView();

        if (firstVisibleItem < listView.getHeaderViewsCount()) {
            mTopView.setVisibility(GONE);
            return;
        }

        firstVisibleItem -= listView.getHeaderViewsCount();

        mTopView.setVisibility(VISIBLE);

        ItemPosition itemPosition = adapter.getRealPosition(firstVisibleItem);

        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mTopView.getLayoutParams();
        if (mCurrentGroup != itemPosition.group) {
            mCurrentGroup = itemPosition.group;
            callTopViewUpdate(mCurrentGroup);
            lp.topMargin = 0;
            mTopView.requestLayout();
            // mTopView.setVisibility(VISIBLE);
        } else if (itemPosition.pos >= adapter.getChildrenCount(mCurrentGroup) - 1) {
            int firstBottom = view.getChildAt(0).getBottom();
            int topHeight = mTopView.getHeight();

            if (firstBottom < topHeight) {
                lp.topMargin = firstBottom - topHeight;
                mTopView.requestLayout();
            } else if (lp.topMargin != 0) {
                lp.topMargin = 0;
                mTopView.requestLayout();
            }
            // System.out.println(view.getChildAt(0).getTop());
        } else if (lp.topMargin != 0) {
            lp.topMargin = 0;
            mTopView.requestLayout();
        }
        // .setLayoutParams(lp);

    }

    private void callTopViewUpdate(int group) {
        if (mTopViewCallback != null) {
            mTopViewCallback.updateView(mTopView.getChildAt(0), group);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    public interface TopViewCallback {
        public View createView();

        public void updateView(View view, int group);
    }

    // private PullRefreshListView mPullRefreshListView;

    // @Override
    // public boolean onTouch(MotionEvent ev) {
    // return mListView.onTouch(ev);
    // }
    //
    // @Override
    // public boolean clearTouch(MotionEvent ev) {
    // return mListView.clearTouch(ev);
    // }
    //
    // @Override
    // public boolean dispatchTouchEvent(MotionEvent ev) {
    // return mListView.dispatchTouchEvent(ev);
    // }
    //
    // @Override
    // public void setPullRefreshListView(PullRefreshListView
    // pullRefreshListView) {
    // // mPullRefreshListView = pullRefreshListView;
    // mListView.setPullRefreshListView(pullRefreshListView);
    // }
    //
    // @Override
    // public void setRefreshViewForList(View refreshView) {
    // mListView.setRefreshViewForList(refreshView);
    // }
}
